﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class MakerController : BaseController
    {
        
		#region admin manager
		public ActionResult Manager()
        {
            return View(new ManagerModel { Title = "Maker manager" });
        }

        [AjaxOrChildAction]
        public ActionResult List()
        {
            return PartialView(RsDataManager.GetList<MakerInfo>("MakerGetAll"));
        }

        [AjaxOrChildAction]
        public ActionResult BrandSearch()
        {
            return PartialView("BrandSearch", RsDataManager.GetList<MakerInfo>("MakerGetAll"));
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int id)
        {
            MakerInfo info = RsDataManager.GetInfo<MakerInfo>("MakerGetInfo", "MakerID", id);
            if (RsDataManager.Delete("MakerDelete", "MakerID", id))
            {
                DeleteFile(info.Image);
                AddMessage(Message.DeleteSuccess);
            }
            else
            {
                AddMessage(Message.DeleteError);
            }
            return RedirectToAction("List");
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Create()
        {
            return PartialView();
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(MakerInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(NewImage))
            {
                return AjaxMessage("Please choose logo");
            }
            if (ModelState.IsValid)
            {
                info.Image = SaveImage(null);
                if (RsDataManager.Save("MakerSave", info)>0)
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id)
        {
            return PartialView(RsDataManager.GetInfo<MakerInfo>("MakerGetInfo", "MakerID", id));
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(MakerInfo info, string NewImage)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrWhiteSpace(NewImage))
                {
                    info.Image = SaveImage(null);
                }
                if (RsDataManager.Save("MakerSave", info)>0)
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }
        [AjaxOrChildAction]
        public MvcHtmlString SelectList(bool? validate, int? selectedValue)
        {
            var list = RsDataManager.GetList<MakerInfo>("MakerGetAll") ?? new List<MakerInfo>();
            TagBuilder select = new TagBuilder("select");
            select.Attributes["name"] = "MakerID";
            select.GenerateId("MakerID");
            if (validate.HasValue && validate.Value)
            {
                select.Attributes["data-val"] = "true";
                select.Attributes["data-val-required"] = "The field is required.";
            }
            TagBuilder option = new TagBuilder("option");
            option.Attributes["value"] = "";
            select.InnerHtml += option.ToString();
            foreach (var item in list)
            {
                option = new TagBuilder("option");
                option.Attributes["value"] = item.MakerID.ToString();
                option.InnerHtml = item.Name;
                if (item.MakerID == (selectedValue ?? 0))
                {
                    option.Attributes["selected"] = "selected";
                }
                select.InnerHtml += option.ToString();
            }
            return MvcHtmlString.Create(select.ToString());
        }
        [AjaxOrChildAction]
        public MvcHtmlString GetName(int id)
        {
            var info = RsDataManager.GetInfo<MakerInfo>("MakerGetInfo", "MakerID", id);
            if (info == null) return MvcHtmlString.Empty;
            return MvcHtmlString.Create(info.Name);
        }
		#endregion
    }
}
